package com.jacob.androidframe.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UtilDateTime
{

    private static long          mNowLong      = System.currentTimeMillis();
    private static Calendar      mNowCalendar  = Calendar.getInstance();
    private static Date          mNowDate      = new Date(mNowLong);
    private static Timestamp     mNowTimestamp = new Timestamp(mNowLong);
    private static java.sql.Date mToday        = new java.sql.Date(mNowLong);
    private static java.sql.Time mNowTime      = new java.sql.Time(mNowLong);

    private UtilDateTime()
    {
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的年。
     * 
     * @param pCalendar
     * @return 当前系统时区下的年。若传入null，返回-1；
     */
    public static int getYear(Calendar pCalendar)
    {
        int _Year = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            _Year = _Calendar.get(Calendar.YEAR);
        }

        return _Year;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的月份。
     * 返回的月份是从1开始的。
     * 
     * @param pCalendar
     * @return 当前系统时区下的月。若传入null，返回-1；
     */
    public static int getMonth(Calendar pCalendar)
    {
        int _Month = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            int _MonthOfYear = _Calendar.get(Calendar.MONTH);

            if (_MonthOfYear == Calendar.JANUARY) {
                _Month = 1;
            } else if (_MonthOfYear == Calendar.FEBRUARY) {
                _Month = 2;
            } else if (_MonthOfYear == Calendar.MARCH) {
                _Month = 3;
            } else if (_MonthOfYear == Calendar.APRIL) {
                _Month = 4;
            } else if (_MonthOfYear == Calendar.MAY) {
                _Month = 5;
            } else if (_MonthOfYear == Calendar.JUNE) {
                _Month = 6;
            } else if (_MonthOfYear == Calendar.JULY) {
                _Month = 7;
            } else if (_MonthOfYear == Calendar.AUGUST) {
                _Month = 8;
            } else if (_MonthOfYear == Calendar.SEPTEMBER) {
                _Month = 9;
            } else if (_MonthOfYear == Calendar.OCTOBER) {
                _Month = 10;
            } else if (_MonthOfYear == Calendar.NOVEMBER) {
                _Month = 11;
            } else if (_MonthOfYear == Calendar.DECEMBER) {
                _Month = 12;
            }

        }

        return _Month;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的日期。
     * 
     * @param pCalendar
     * @return 当前系统时区下的日期。若传入null，返回-1；
     */
    public static int getDate(Calendar pCalendar)
    {
        int _Date = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            _Date = _Calendar.get(Calendar.DATE);
        }

        return _Date;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的小时（24小时制）。
     * 
     * @param pCalendar
     * @return 当前系统时区下的小时（24小时制）。若传入null，返回-1；
     */
    public static int getHour(Calendar pCalendar)
    {
        int _Hour = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            _Hour = _Calendar.get(Calendar.HOUR_OF_DAY);
        }

        return _Hour;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的分钟。
     * 
     * @param pCalendar
     * @return 当前系统时区下的分钟。若传入null，返回-1；
     */
    public static int getMinute(Calendar pCalendar)
    {
        int _Minute = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            _Minute = _Calendar.get(Calendar.MINUTE);
        }

        return _Minute;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的秒。
     * 
     * @param pCalendar
     * @return 当前系统时区下的秒。若传入null，返回-1；
     */
    public static int getSecond(Calendar pCalendar)
    {
        int _Second = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            _Second = _Calendar.get(Calendar.SECOND);
        }

        return _Second;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的毫秒。
     * 
     * @param pCalendar
     * @return 当前系统时区下的毫秒。若传入null，返回-1；
     */
    public static int getMilliSecond(Calendar pCalendar)
    {
        int _MilliSecond = -1;

        if (pCalendar != null) {
            Calendar _Calendar = Calendar.getInstance();
            _Calendar.setTimeInMillis(pCalendar.getTimeInMillis());
            _MilliSecond = _Calendar.get(Calendar.MILLISECOND);
        }

        return _MilliSecond;

    }

    /**
     * 传入Calendar对象，返回当前系统时区下的日期字符串。
     * YYYY-MM-DD格式，例如：2012-05-06
     * 
     * @param pCalendar
     * @return 当前系统时区下的日期字符串。若传入null，返回长度为0的空字符串；
     */
    public static String getYearMonthDate1(Calendar pCalendar)
    {
        String _YearMonthDate = "";
        
        if (pCalendar != null) {
            SimpleDateFormat _SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            _YearMonthDate = _SimpleDateFormat.format(pCalendar.getTime());
        }
        
        return _YearMonthDate;
    }
    
    /**
     * 传入Calendar对象，返回当前系统时区下的日期字符串。
     * YYYY年MM月DD日格式，例如：2012年05月06日
     * 
     * @param pCalendar
     * @return 当前系统时区下的日期字符串。若传入null，返回长度为0的空字符串；
     */
    public static String getYearMonthDate2(Calendar pCalendar)
    {
        String _YearMonthDate = "";

        if (pCalendar != null) {
            SimpleDateFormat _SimpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            _YearMonthDate = _SimpleDateFormat.format(pCalendar.getTime());
        }
        
        return _YearMonthDate;
    }

    /**
     * 传入Calendar对象，返回当前系统时区下的日期字符串。
     * YYYYMMDD格式，例如：20120506
     * 
     * @param pCalendar
     * @return 当前系统时区下的日期字符串。若传入null，返回长度为0的空字符串；
     */
    public static String getYearMonthDate3(Calendar pCalendar)
    {
    	String _YearMonthDate = "";
    	
    	if (pCalendar != null) {
    		SimpleDateFormat _SimpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    		_YearMonthDate = _SimpleDateFormat.format(pCalendar.getTime());
    	}
    	
    	return _YearMonthDate;
    }
    
    /**
     * 得到指定或者当前时间前n天的Calendar
     * 
     * @param c
     * @param n
     * @return
     */
    public static Calendar beforeNDays(Calendar c, int n)
    {
        // 偏移量，给定n天的毫秒数
        long offset = n * 24 * 60 * 60 * 1000;
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }

        calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
        return calendar;
    }

    /**
     * 得到指定或者当前时间后n天的Calendar
     * 
     * @param c
     * @param n
     * @return
     */
    public static Calendar afterNDays(Calendar c, int n)
    {
        // 偏移量，给定n天的毫秒数
        long offset = n * 24 * 60 * 60 * 1000;
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }

        calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
        return calendar;
    }

    /**
     * 昨天
     * 
     * @param c
     * @return
     */
    public static Calendar yesterday(Calendar c)
    {
        long offset = 1 * 24 * 60 * 60 * 1000;
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }

        calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
        return calendar;
    }

    /**
     * 明天
     * 
     * @param c
     * @return
     */
    public static Calendar tomorrow(Calendar c)
    {
        long offset = 1 * 24 * 60 * 60 * 1000;
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }

        calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
        return calendar;
    }

    /**
     * 得到指定或者当前时间前offset毫秒的Calendar
     * 
     * @param c
     * @param offset
     * @return
     */
    public static Calendar before(Calendar c, long offset)
    {
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }

        calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
        return calendar;
    }

    /**
     * 得到指定或者当前时间前offset毫秒的Calendar
     * 
     * @param c
     * @param offset
     * @return
     */
    public static Calendar after(Calendar c, long offset)
    {
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }

        calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
        return calendar;
    }

    /**
     * 日期格式化
     * 
     * @param c
     * @param pattern
     * @return
     */
    public static String format(Calendar c, String pattern)
    {
        Calendar calendar = null;
        if (c != null) {
            calendar = c;
        } else {
            calendar = Calendar.getInstance();
        }
        if (pattern == null || pattern.equals("")) {
            pattern = "yyyy年MM月dd日 HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        return sdf.format(calendar.getTime());
    }

    /**
     * Date类型转换到Calendar类型
     * 
     * @param d
     * @return
     */
    public static Calendar Date2Calendar(Date d)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c;
    }

    /**
     * Calendar类型转换到Date类型
     * 
     * @param c
     * @return
     */
    public static Date Calendar2Date(Calendar c)
    {
        return c.getTime();
    }

    /**
     * Date类型转换到Timestamp类型
     * 
     * @param d
     * @return
     */
    public static Timestamp Date2Timestamp(Date d)
    {
        return new Timestamp(d.getTime());
    }

    /**
     * Calendar类型转换到Timestamp类型
     * 
     * @param c
     * @return
     */
    public static Timestamp Calendar2Timestamp(Calendar c)
    {
        return new Timestamp(c.getTimeInMillis());
    }

    /**
     * Timestamp类型转换到Calendar类型
     * 
     * @param ts
     * @return
     */
    public static Calendar Timestamp2Calendar(Timestamp ts)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(ts);
        return c;
    }

    /**
     * 得到当前时间的字符串表示
     * 格式2010-02-02 12:12:12
     * 
     * @return
     */
    public static String getTimeString()
    {
        return format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 标准日期格式字符串解析成Calendar对象
     * 
     * @param s
     * @return
     */
    public static Calendar pars2Calender(String s)
    {
        Timestamp ts = Timestamp.valueOf(s);
        return Timestamp2Calendar(ts);
    }

    // ================以下是get和set方法=========================//

    public long getLNow()
    {
        return mNowLong;
    }

    public void setLNow(long now)
    {
        mNowLong = now;
    }

    public Calendar getCNow()
    {
        return mNowCalendar;
    }

    public void setCNow(Calendar now)
    {
        mNowCalendar = now;
    }

    public Date getDNow()
    {
        return mNowDate;
    }

    public void setDNow(Date now)
    {
        mNowDate = now;
    }

    public Timestamp getTNow()
    {
        return mNowTimestamp;
    }

    public void setTNow(Timestamp now)
    {
        mNowTimestamp = now;
    }

    public java.sql.Date getToday()
    {
        return mToday;
    }

    public void setToday(java.sql.Date today)
    {
        this.mToday = today;
    }

    public java.sql.Time getNow()
    {
        return mNowTime;
    }

    public void setNow(java.sql.Time now)
    {
        this.mNowTime = now;
    }

}
